package thinking.in.spring.ioc.overview.dependency.lookup;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import thinking.in.spring.ioc.overview.domain.User;

import java.io.IOException;

/**
 * 延迟依赖查找案例
 *
 * @author Ringo
 * @date 2021/8/31 10:24
 */
public class DependencyDelayLookupDemo {

    public static void main(String[] args) {
        // 创建 IoC 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.scan("thinking.in.spring.ioc.overview.dependency.lookup.**");
        applicationContext.refresh();

        ObjectFactory<User> objectFactory = (ObjectFactory<User>) applicationContext.getBean(ObjectFactory.class);
        System.out.println(objectFactory);
        System.out.println(objectFactory.getObject());

        applicationContext.close();
    }

    @Configuration
    public static class Config {
        @Bean
        public User user() {
            return new User();
        }

        @Bean
        public ObjectFactoryCreatingFactoryBean objectFactory() {
            ObjectFactoryCreatingFactoryBean objectFactory = new ObjectFactoryCreatingFactoryBean();
            objectFactory.setTargetBeanName("user");
            return objectFactory;
        }
    }

}
